<template>
  <div>
    <el-button type="primary" icon="el-icon-plus" @click="showDialog"
      >添加房间</el-button
    >

    <el-dialog
      :title="cform.id ? '修改房间' : '添加房间'"
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="cform">
        <el-form-item label="房间号" :label-width="formLabelWidth">
          <el-input v-model="cform.room" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="房间类型" :label-width="formLabelWidth">
          <el-select v-model="value" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.remark"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="介绍" :label-width="formLabelWidth">
          <el-input type="textarea" maxlength="500"
                    :rows="4" v-model="cform.context" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="房价" :label-width="formLabelWidth">
          <el-input v-model="cform.money" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="折扣" :label-width="formLabelWidth">
          <el-input v-model="cform.discount" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrUpdateUser">确 定</el-button>
      </div>
    </el-dialog>

    <!-- <div style="margin-top: 15px">
      <el-input
        placeholder="请输入内容"
        v-model="input"
        class="input-with-select"
      >
        <el-select v-model="select" slot="prepend" placeholder="请选择">
          <el-option label="账号" value="1"></el-option>
          <el-option label="手机号" value="2"></el-option>
        </el-select>
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="getUserBy"
        ></el-button>
      </el-input>
    </div> -->

    <el-table :data="Form" style="width: 100%; margin: 20px 0px" border>
      <el-table-column
        type="index"
        label="序号"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column prop="room" label="房间号"> </el-table-column>
      <el-table-column
        prop="roomParams.remark"
        label="房间类型"
      ></el-table-column>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="founder" label="创建人"> </el-table-column>
      <el-table-column prop="state" label="是否入住"> </el-table-column>
      <el-table-column prop="discount" label="折扣"> </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="{ row, $index }">
          <el-button
            type="primary"
            icon="el-icon-edit"
            @click="updateUser(row)"
            circle
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click="open(row)"
            circle
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 20px; textalign: center"
      :current-page="pages.page"
      :total="total"
      :page-size="pages.pageSize"
      :pager-count="7"
      :page-sizes="[5, 10, 20]"
      @current-change="getPageList"
      @size-change="handleSizeChange"
      layout="prev,pager,next,jumper,->,total,sizes"
    ></el-pagination>
  </div>
</template>
<script>
export default {
  name: "sysUser",
  data() {
    return {
      dialogFormVisible: false,
      formLabelWidth: "120px",
      total: 0,
      pages: {
        pageSize: 5,
        page: 1,
      },
      cform: {
        id: "",
        room: "",
        number: "",
        money: "",
        context: "",
        creationTime: "",
        founder: "",
        discount: "",
        roomType: "",
        roomtypes: "",
      },
      Form: [],
      input: "",
      select: "",
      tableData: "",
      // visible: false,
      options: [],
      value: "",
    };
  },
  mounted() {
    this.findTablePages();
    this.findRoomTypeAll();
  },
  methods: {
    async open(row) {
      console.log(row.id)
      let result = await this.$API.table.reqdeleteTable(row.id)
      if(result.code == 20000){
        this.$message({
          type: "success",
          message: "删除成功",
        });
        this.findTablePages();
      }
    },
    updateUser(row) {
      this.dialogFormVisible = true;
      this.cform = row;
      this.cform = { ...row };
      console.log(row);
    },
    getPageList(pager) {
      this.findTablePages(pager);
    },
    handleSizeChange(pageSize) {
      this.pages.pageSize = pageSize;
      this.findTablePages();
    },
    async findRoomTypeAll() {
      let result = await this.$API.roomType.reqfindRoomTypeAll();
      console.log(result);
      if (result.code == 20000) {
        this.options = result.data;
      }
    },
    showDialog() {
      this.cform = {};
      this.dialogFormVisible = true;
    },
    async findTablePages(page = 1) {
      this.pages.page = page;
      let result = await this.$API.table.requfindTablePages(this.pages);
      console.log(result);
      if (result.code == 20000) {
        this.Form = result.data.records;
        this.total = result.data.total;
      }
    },
    async addOrUpdateUser() {
      this.cform.roomType = this.value;
      let result = await this.$API.table.reqaddTables(this.cform);
      console.log(result);
      if (result.code == 20000) {
        this.dialogFormVisible = false;
        this.$message({
          type: "success",
          message: this.cform.id ? "添加房间成功" : "修改房间成功",
        });
        this.getPageList(this.cform.id ? this.pages.page : 1);
      }
    },
  },
};
</script>
<style>
.el-select .el-input {
  width: 130px;
}
.input-with-select .el-input-group__prepend {
  background-color: #fff;
}
</style>
